<?php

// 由于 UDP 服务在 SDK 开发时无服务端，所以目前本接口仅实现应用接口的调用

namespace Lib;

use Exception;
use Sdk\lib\Core;

class Udp {

	const MaxLength = 65507; // 64k

	public static function SendRecv($name, $message, $timeout = 0.5) {
		list($server, $method) = explode('_', $name);
		$class = $server . 'Server';
		$method = $method . 'Udp';
		$server = ucfirst($server);
		$f = PLUGIN_PATH . 'servers/' . $server . '.php';

		try {
			require_once $f;
			$c = new $class();
			$return = call_user_func_array([$c, $method], [\Common::random(10, 1), $message, '']);
		} catch (Exception $e) {
			return false;
		};
		return $return;
	}

	public static function Send($name, $message, $timeout = 0.5) {
		$fakeClient = \Common::random(10);
		$GLOBALS['Udp_' . $fakeClient] = self::SendRecv($name, $message, $timeout);
		return $fakeClient;
	}

	public static function Recv($fakeClient) {
		return $GLOBALS['Udp_' . $fakeClient];
	}

}